package com.cms.recursoshumanos.ui.catalogo;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.TipoArticuloListButtonField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.SearchBarManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.ListaArticuloCatalogoScreenHandler;
import com.cms.recursoshumanos.handler.ListaTipoArticuloScreenHandler;
import com.cms.recursoshumanos.object.TipoArticulo;
import com.cms.recursoshumanos.object.list.ListaTipoArticulo;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * @author Francisco Trompiz
 *
 * Corporate Mobile Solutions. 2013
 */

public class ListaTipoArticuloScreen extends RecursosHumanosScreen implements FieldChangeListener 
{

	
	private static ListaTipoArticuloScreen instance = null;
	
	private ListaTipoArticulo listaTipoArticulo;
	
	//SearchBarManager sbm;
	ListStyleButtonSetScrollManager buttonSet;
	VerticalFieldManager vfm;
	TwoColumnFieldManager twoColumnManager;
	

	
	
	public static ListaTipoArticuloScreen getInstance(ListaTipoArticulo listado)
	{
		if(instance == null)
    	{
    		instance = new ListaTipoArticuloScreen(listado);
    	}
		else
		{
			instance.listaTipoArticulo = listado;
			instance.reset();
		}
    	return instance;
    }
	
	
    private ForegroundManager _foreground;    
    
	public ListaTipoArticuloScreen(ListaTipoArticulo listado) 
	{
		super( NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		
		this.listaTipoArticulo = listado;	
		
		setComponents();
	}
			
	
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem refreshButton = new MenuItem("Actualizar", 0, 0) 
        {
			public void run() 
			{
				ListaTipoArticuloScreenHandler handler = new ListaTipoArticuloScreenHandler();
	   			handler.refreshListadoTipoArticulo(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
	   					UserInfoCache.getInstance().getUsuario().getPassword());
			}
		};
		menu.add(refreshButton);
		super.makeMenu(menu, instance);
	}
	
	public void reset() 
	{
		vfm.deleteAll();
		//loading = false;
		try 
		{
			if(buttonSet.getFieldCount() > 0) 
			{
				//sbm.setFocus();
				buttonSet.deleteAll();
			}
		} 
		catch (Exception e) 
		{
			//ShowDialogAlert alert = new ShowDialogAlert("Exception in delete fields...");
		}
		showListField();
		//pis = Json.getInstance().getPIs();
	}
	public void getPIs() 
	{
		//pis = JsonObjectPIs.getInstance().getPIs();
	}
	public void setComponents() 
	{
		setTitle("Catalogo");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE  );
		_foreground = new ForegroundManager(NO_VERTICAL_SCROLL);
		
		twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 6, 6, 6, 6 );
        twoColumnManager.setPadding( 2, 2, 2, 2 );
		
        buttonSet = new ListStyleButtonSetScrollManager();
        buttonSet.setScreen(this);
        
		//sbm = new SearchBarManager();
		LabelField lbl_mensaje = new LabelField("Elija entre las Categorias", LabelField.HCENTER | FIELD_HCENTER);
		twoColumnManager.add ( lbl_mensaje );
		Field separator = new SeparatorField();
        separator.setMargin( 2, 0, 2, 0 );
        twoColumnManager.add( separator );
        
        //LabelField tipo_menu = new LabelField("Menu de "+this.elMenu.getTipoMenu().getNombreTipoMenu());
        //String fecha_seleccionada = tools.transformDateFormat(this.elMenu.getFechaMenu());
        
        showListField();
		twoColumnManager.add(buttonSet);
		
        //_foreground.add(sbm);
        _foreground.add( twoColumnManager );
        add( _foreground );
	}
	public void getLinkTodos()
	{
		TipoArticuloListButtonField link;
		link = new TipoArticuloListButtonField(new TipoArticulo(0,"Todas las Categorias",null), LabelField.ELLIPSIS) 
		{
		
			protected void paintBackground( Graphics g )
			{
				  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
				  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

			      if( _drawPosition < 0 ) 
			      {
			          // it's like a list field, let the default background be drawn
			          super.paintBackground( g );
			          return;
			      }
			      
			      int oldColour = g.getColor();
			      
			      int focus = COLOR_BACKGROUND_FOCUS;
			      if(_doAction) 
			    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
			      
			      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
			      try 
			      {
			          if( _drawPosition == 0 ) 
			          {
			              // Top
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else if( _drawPosition == 1 ) 
			          {
			              // Bottom 
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			          } 
			          else if( _drawPosition == 2 ) 
			          {
			              // Middle
			              g.setColor( background );
			              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
			              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
			          } 
			          else 
			          {
			              // Single
			              g.setColor( background );
			              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			              g.setColor( COLOR_BORDER );
			              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
			          }
			      } 
			      finally 
			      {
			          g.setColor( oldColour );
			      }
			}
		};
		link.setChangeListener( this );
		buttonSet.add( link );
	}
	public void showListField()
	{
		buttonSet.deleteAll();
		TipoArticuloListButtonField link;
		this.getLinkTodos();
		ListaTipoArticulo lista_tipo_articulo = this.listaTipoArticulo;
		for(int i = 0; i < lista_tipo_articulo.cantElementListaTipoArticulo() ; i++) 
		{

			link = new TipoArticuloListButtonField(lista_tipo_articulo.getTipoArticuloAtIndex(i), LabelField.ELLIPSIS) 
			{
			
				protected void paintBackground( Graphics g )
				{
					  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
					  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

				      if( _drawPosition < 0 ) 
				      {
				          // it's like a list field, let the default background be drawn
				          super.paintBackground( g );
				          return;
				      }
				      
				      int oldColour = g.getColor();
				      
				      int focus = COLOR_BACKGROUND_FOCUS;
				      if(_doAction) 
				    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
				      
				      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
				      try 
				      {
				          if( _drawPosition == 0 ) 
				          {
				              // Top
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else if( _drawPosition == 1 ) 
				          {
				              // Bottom 
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				          } 
				          else if( _drawPosition == 2 ) 
				          {
				              // Middle
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else 
				          {
				              // Single
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				          }
				      } 
				      finally 
				      {
				          g.setColor( oldColour );
				      }
				}
			};
			
			link.setChangeListener( this );
			buttonSet.add( link );
		}
		/*if(from == 0 && pis.size() >= 1) 
		{
//			if(from == 0 && pis.size() < Integer.parseInt(BaseFacade.NUM_ITEMS)) {
//				
//			} else {
//				buttonSet.add(new LoadingListStyleButtonField(loadingImage));
//			}
			buttonSet.setFocus();
		}*/
	}
	public void fieldChanged( Field field, int context ) 
	{
		ListaArticuloCatalogoScreenHandler handler = new ListaArticuloCatalogoScreenHandler(field.getIndex());
		handler.getListaArticuloScreen(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
				UserInfoCache.getInstance().getUsuario().getPassword());
	}	
}


